﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using ThingPower.WeChat.MP.CommonAPIs;
using ThingPower.WeChat.MP.AdvancedAPIs;
using ThingPower.WeChat.MP.AdvancedAPIs.TemplateMessage;
using ThingPower.WeChat.Entities;

namespace WeChatDemo
{
    /// <summary>
    /// doEnroll 的摘要说明
    /// </summary>
    public class doEnroll : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strEmail = context.Request["email"];
            string strPassword = context.Request["password"];
            string strOpenId = context.Request["openid"];

            string appId = "wx71dae64ee25e0920";
            string appSecret = "4ab5fe683697520c7edb5cd233497a9c";
            var result = AccessTokenContainer.TryGetToken(appId, appSecret);

            var data = new
            {
                course = new TemplateDataItem("快乐星期四")
            };

            string templateId = "JOfiDs0PCxQoaa-unOJJKF9iURtCjJNn_hYD3hZy710";
            //string openId = "oRMP5shI6hdqW7Fs7quu9yuU3HKg";
            string topColor = "#FF0000";
            string url = "http://www.shtraining.net";

            //SendTemplateMessageResult sendResult = TemplateApi.SendTemplateMessage(result.ToString(), "oRMP5shI6hdqW7Fs7quu9yuU3HKg", "JOfiDs0PCxQoaa-unOJJKF9iURtCjJNn_hYD3hZy710", "#ff0000", "http://www.shtraining.net", data);

            const string urlFormat = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}";
            var msgData = new TempleteModel()
            {
                template_id = templateId,
                topcolor = topColor,
                touser = strOpenId,
                data = data,
                url = url
            };
            CommonJsonSend.Send<WxJsonResult>(result.ToString(), urlFormat, msgData);

            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello Wechat!" + strEmail + "," + strPassword + "," + result);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}